//
//  UICustomTabbar.m
//  WorkFrame
//
//  Created by tiannu on 15/11/7.
//  Copyright (c) 2015年 tiannu. All rights reserved.

#import "UICustomTabbar.h"

@interface UICustomTabbarItem : UIControl

@property (nonatomic, strong) UIImage *backgroundImage;
@property (nonatomic, strong) NSString *iconImage;
@property (nonatomic, strong) NSString *title;

@property (nonatomic, strong) UIImageView *backgroundImageV;
@property (nonatomic, strong) UIImageView *iconImageV;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation UICustomTabbarItem

- (void)setBackgroundImage:(UIImage *)backgroundImage {
    _backgroundImage = backgroundImage;
    
    if (!self.backgroundImageV) {
        self.backgroundImageV = [[UIImageView alloc] initWithFrame:self.bounds];
        self.backgroundImageV.backgroundColor = [UIColor clearColor];
        [self addSubview:self.backgroundImageV];
    }
    
    self.backgroundImageV.image = backgroundImage;
}

- (void)setIconImage:(NSString *)iconImage {
    _iconImage = iconImage;
    
    if (!self.iconImageV) {
        self.iconImageV = [[UIImageView alloc] init];
        self.iconImageV.backgroundColor = [UIColor clearColor];
        [self addSubview:self.iconImageV];
    }
    
    self.iconImageV.frame = CGRectMake(0, 0, 25, 25);
    self.iconImageV.center = CGPointMake(CGRectGetWidth(self.bounds) / 2, 20);
    if ([iconImage containString:@"http"]) {
        [self.iconImageV sd_setImageWithURL:KURL(iconImage)];
    }else{
        self.iconImageV.image = [UIImage imageNamed:iconImage];
    }
}

- (void)setTitle:(NSString *)title {
    _title = title;
    if ([title isEqualToString:@""]) {
        return;
    }
    if (!self.titleLabel) {
        CGFloat h = 16;
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - h, CGRectGetWidth(self.bounds), h)];
        self.titleLabel.backgroundColor = [UIColor clearColor];
        self.titleLabel.font = [UIFont systemFontOfSize:11];
        self.titleLabel.textColor = [UIColor blackColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.titleLabel];
    }
    self.titleLabel.text = title;
}

@end

@implementation UICustomTabbar {
    NSMutableArray *tabBaritems;
}

@synthesize textSelectedColor = _textSelectedColor;
@synthesize textNormalColor = _textNormalColor;

+ (UICustomTabbar *)customTabbar {
    UICustomTabbar *tabbar = [[UICustomTabbar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 49)];
    tabbar.backgroundColor = [UIColor clearColor];
    return tabbar;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        tabBaritems = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)setTitles:(NSArray *)titles {
    _titles = titles;
    [self setUpItems];
}

- (void)setNormalImages:(NSArray *)normalImages {
    _normalImages = normalImages;
    [self setUpItems];
}

- (void)setSelectedImages:(NSArray *)selectedImages {
    _selectedImages = selectedImages;
    [self setUpItems];
}

- (void)setNormalBackgroundImages:(NSArray *)normalBackgroundImages {
    _normalBackgroundImages = normalBackgroundImages;
    [self setUpItems];
}

- (void)setSelectedBackgroundImages:(NSArray *)selectedBackgroundImages {
    _selectedBackgroundImages = selectedBackgroundImages;
    [self setUpItems];
}

- (void)setSelectedIndex:(NSInteger)selectedIndex {
    _selectedIndex = selectedIndex;
    [self setUpItems];
}

- (void)setTextNormalColor:(UIColor *)textNormalColor {
    _textNormalColor = textNormalColor;
    [self setUpItems];
}

- (void)setTextSelectedColor:(UIColor *)textSelectedColor {
    _textSelectedColor = textSelectedColor;
    [self setUpItems];
}

- (UIColor *)textNormalColor {
    if (_textNormalColor == nil) {
        _textNormalColor = [UIColor blackColor];
    }
    return _textNormalColor;
}

- (UIColor *)textSelectedColor {
    if (_textSelectedColor == nil) {
        _textSelectedColor = [UIColor blackColor];
    }
    return _textSelectedColor;
}

- (void)setUpItems {
    if (self.counts <= 0) {
        return;
    }
    
    CGFloat item_w = CGRectGetWidth(self.bounds) / self.counts;
    for (NSInteger i = 0; i < self.counts; i++) {
        if (tabBaritems.count <= i) {
            UICustomTabbarItem *item = [[UICustomTabbarItem alloc] initWithFrame:CGRectMake(i * item_w, 0, item_w, CGRectGetHeight(self.bounds))];
            item.tag = i;
            
            [item addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchDown];
            [self addSubview:item];
            [tabBaritems addObject:item];
        }
        
        [self changeItemStyle:i];
    }
}

- (void)itemClick:(id)sender {
    UIControl *nowView = (UIControl *)sender;
    
    NSInteger index = nowView.tag;
    self.selectedIndex = index;
    [self changeItemStyle:index];
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectItemAtIndex:)]) {
        [self.delegate tabBar:self didSelectItemAtIndex:index];
    }
}

- (void)changeItemStyle:(NSInteger)index {
    UICustomTabbarItem *item = tabBaritems[index];
    if (self.selectedIndex == index) {
        item.backgroundImage = self.selectedBackgroundImages.count > index ? [UIImage imageNamed:self.selectedBackgroundImages[index]] : nil;

        item.iconImage = self.selectedImages.count > index ? self.selectedImages[index] : nil;
        item.title = self.titles.count > index ? self.titles[index] : @"";
        item.titleLabel.textColor = self.textSelectedColor;
    }
    else {
        item.backgroundImage = self.normalBackgroundImages.count > index ? [UIImage imageNamed:self.normalBackgroundImages[index]] : nil;
        item.iconImage = self.normalImages.count > index ? self.normalImages[index] : nil;
        item.title = self.titles.count > index ? self.titles[index] : @"";
        item.titleLabel.textColor = self.textNormalColor;
    }
}

@end
